package fr.unice.outils;

import java.util.ArrayList;

import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.YouTubeMediaGroup;

public class Panier {
        private ArrayList<String> lesTag;
        private ArrayList<String> tagSelection;
        private ArrayList<Integer> tagCpt;
        private ArrayList<VideoEntry> videoList;
        private ArrayList<VideoEntry> suggestion;
        public static final String VIDEOS_FEED = "http://gdata.youtube.com/feeds/api/videos";

        public Panier(ArrayList<String> lesTag, ArrayList<String> tagSelection,
                        ArrayList<Integer> tagCpt, ArrayList<VideoEntry> videoList,
                        ArrayList<VideoEntry> suggestionList) {
                this.lesTag = lesTag;
                this.tagSelection = tagSelection;
                this.tagCpt = tagCpt;
                this.videoList = videoList;
                this.suggestion = suggestionList;
        }

        public void addVideo(VideoEntry video) {
                boolean contient = false;
                for (int i = 0; i < videoList.size(); i++) {
                        if (videoList.get(i).getHtmlLink().getHref().compareToIgnoreCase(
                                        video.getHtmlLink().getHref()) == 0) {
                                contient = true;
                        }
                }
                if (!contient) {
                        YouTubeMediaGroup mediaGroup = video.getMediaGroup();
                        if (mediaGroup != null) {
                                MediaKeywords keywords = mediaGroup.getKeywords();
                                for (String keyword : keywords.getKeywords()) {
                                        keyword = keyword.toLowerCase();
                                        addTag(keyword);
                                }

                        }
                        videoList.add(video);
                }
        }

        public void addSuggestion(VideoEntry video) {
                boolean contient = false;
                for (int i = 0; i < videoList.size(); i++) {
                        if (videoList.get(i).getHtmlLink().getHref().compareToIgnoreCase(
                                        video.getHtmlLink().getHref()) == 0) {
                                contient = true;
                        }
                }
                if (contient!=true) {
                        suggestion.add(video);
                }

        }

        public ArrayList<VideoEntry> getSuggestion() {
                return suggestion;
        }

        public void cleanSuggestion() {
                suggestion.clear();
        }
        public void cleanPanier() {
                lesTag.clear();
                tagSelection.clear();
                tagCpt.clear();
                videoList.clear();
        }

        public void deleteVideo(VideoEntry video) {
                boolean contient = false;
                for (int i = 0; i < videoList.size(); i++) {
                        if (videoList.get(i).getHtmlLink().getHref().compareToIgnoreCase(
                                        video.getHtmlLink().getHref()) == 0) {
                                YouTubeMediaGroup mediaGroup = video.getMediaGroup();
                                if (mediaGroup != null) {
                                        MediaKeywords keywords = mediaGroup.getKeywords();
                                        for (String keyword : keywords.getKeywords()) {
                                                keyword = keyword.toLowerCase();
                                                delTag(keyword);
                                        }

                                }
                                videoList.remove(i);
                        }
                }
        }

        public void addTag(String tag) {
                if (lesTag.contains(tag)) {
                        if (tagSelection.contains(tag)) {
                                tagCpt.set(tagSelection.indexOf(tag), tagCpt.get(tagSelection
                                                .indexOf(tag)) + 1);

                        } else {
                                tagSelection.add(tag);
                                tagCpt.add(2);
                        }
                } else {
                        lesTag.add(tag);
                }
        }

        public void delTag(String tag) {
                if (lesTag.contains(tag)) {
                        if (tagSelection.contains(tag)) {
                                if (tagCpt.get(tagSelection.indexOf(tag)) > 2) {
                                        tagCpt.set(tagSelection.indexOf(tag), tagCpt
                                                        .get(tagSelection.indexOf(tag)) - 1);
                                } else {
                                        tagCpt.remove(tagSelection.indexOf(tag));
                                        tagSelection.remove(tagSelection.indexOf(tag));
                                }
                        } else {
                                if (lesTag.contains(tag)) {
                                        lesTag.remove(tag);
                                }

                        }
                }
        }

        public ArrayList<VideoEntry> getVideoList() {
                return videoList;
        }

        public ArrayList<String> getTagSelection() {
                return tagSelection;
        }

        public ArrayList<String> getAllTag() {
                return lesTag;
        }

}
